package com.uh.calculation.inter.imp;

import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.bean.OperatorVal;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.operator.*;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.GetOrganization;
import com.uh.calculation.util.OperatorXmlUtil;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.summary.enums.RuleEnum;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MaoyiImp implements Warn {

    private JdbcDao jdbcDao;
    /**
     * 业务变量配置
     *
     * @param warnJiSuan
     * @return
     */
    //////////////////////////////////////////////////////////////修改
    //修改频次
    private String xgpc = "";

    //1-本月 营业收入修改比例超过15%，计预警因子50。
    //年报营业收入修改比例超过15%，计预警因子50。（此条仅用于4月年报出来时，但一直触发到该企业被核查或检查为止）
    private String  xgfd="[{dimension:\"A\",field:\"$NEW_VALUE;$NEW_VALUE#m\",formulaBeans:[{formula:\"($NEW_VALUE@$NEW_VALUE#m)/$NEW_VALUE#m>0.15|50\"}]}]";

    private String  xgcs="[{dimension:\"A\",field:\"$NEW_VALUE\",formulaBeans:[{formula:\"$NEW_VALUE>3|50\"}]}]";




    //////////////////////////////////////////////////////////////异常
    //运营状态  目前无准确字段
    private String yxzt = "[{dimension:\"B\",field:\"E204-1_M12\",formulaBeans:[{formula:\"2|20\"},{formula:\"4|20\"},{formula:\"5|20\"},{formula:\"6|20\"},{formula:\"7|20\"}]}]";
    //被举报
    private String bjb = "";
    //填表人变动
    private String tbr = "";
    //贸易-批零
//    private String pl = "[{\"dimension\":\"B\",\"field\":\"E104-1_M11;E104-1_M9\",\"formulaBeans\":[{\"formula\":\"E104-1_M9-E104-1_M11>0&E104-1_M9<24000|50\"},{\"formula\":\"(E104-1_M11-E104-1_M9)>0&E104-1_M11<5100|50\"},{\"formula\":\"E204-1_M9<5100|50\"},{\"formula\":\"E204-1_M11<24000|50\"}]}]";
    private String pl = "[{\"dimension\":\"B\",\"field\":\"E104-1_M11;E104-1_M9\",\"formulaBeans\":[{\"formula\":\"E104-1_M9-E104-1_M11>0&E104-1_M9<24000|50\"}]}]";
    //贸易-住餐
    private String zc = "[{dimension:\"B\",field:\"S104-1_M1\",formulaBeans:[{formula:\"E104-1_M<2400|50\"}]}]";


    //////////////////////////////////////////////////////////////增速
    //本月销售额（营业额）(e204-1 M12)同比(去年e204-1 M14)/环比(上个月e204-1 M12)增速增长或下降为30%，计150分，每超过30%一个百分点，加5分。
    private String zs1 = "[{dimension:\"D\",field:\"E204-1_M12;E204-1_M14\",formulaBeans:[{formula:\"((E204-1_M12@E204-1_M14)/E204-1_M14)>0.3&((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150>=300|300\"},{formula:\"((E204-1_M12@E204-1_M14)/E204-1_M14)>0.3&((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150<300|((((E204-1_M12@E204-1_M14)/E204-1_M14)-0.3)*100)*5+150\"}]}]";
    //每季营业收入(e203 M26)增速同比(e203 M27)增长超20%，计50分，每超过20%一个百分点，加2分。（该条只会在每季季报出来后才会触发，一旦触发，持续到该企业被核查或检查，企业被核查或检查后消失）
    private String zs2 = "[{dimension:\"D\",field:\"E203_M26;E203_M27\",formulaBeans:[{formula:\"((E203_M26@E203_M27)/E203_M27)>0.2&((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50>=100|100\"},{formula:\"((E203_M26@E203_M27)/E203_M27)>0.2&((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50<100|((((E203_M26@E203_M27)/E203_M27)-0.2)*100)*2+50\"}]}]";
    //////////////////////////////////////////////////////////////匹配

    //每季末的1-本月销售额(e204-1 M13)/季报上的营业收入(e203 M26)为115%时，计100分，每比115%大一个百分点，加2分。为85%时，计100分，每小85%一个百分点，加2分。
    private String pp1 = "[{dimension:\"E\",field:\"E204-1_M13;E203_M26\",formulaBeans:[{formula:\"E204-1_M13/E203_M26>1.15&((((E204-1_M13/E203_M26)-1.15)*100)*2)+100>=200|200\"},{formula:\"E204-1_M13/E203_M26>1.15&((((E204-1_M13/E203_M26)-1.15)*100)*2)+100<200|((((E204-1_M13/E203_M26)-1.15)*100)*2)+100\"},{formula:\"E204-1_M13/E203_M26<0.85&((((E204-1_M13/E203_M26)@0.85)*100)*2)+100>=200|200\"},{formula:\"E204-1_M13/E203_M26<0.85&((((E204-1_M13/E203_M26)@0.85)*100)*2)+100<200|((((E204-1_M13/E203_M26)@0.85)*100)*2)+100\"}]}]";


    //////////////////////////////////////////////////////////////调整

    //本月商品零售额(e204-1 M20)+上个月的1-本月商品零售额(e204-1 M21)与1-本月商品零售额(e204-1 M21)的差异率为20%，计50分。每超过20%一个百分点，多加1分。如差异率为21%，分数51；增速为25%，分数55分，增速30%，分数60分；增速11.5%，分数为51.5分
    private String tz2 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M20;E204-1_M21#m;E204-1_M21;\",\"formulaBeans\":[{\"formula\":\"(((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)=0.2|50\"},{\"formula\":\"(((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)>0.2|((((E204-1_M20+E204-1_M21#m)/E204-1_M21)-1)-0.2)/0.01*1+50\"}]}]";
    //上一年度12个月的本月商品销售额(e204-1 M12)相加与年报商品销售额(e104-1 M6)的差异率超过20%，计50分。每超过20%一个百分点，多加2分。（一年以内这一条在4月份年报出来的时候使用，然后一直保留这家企业被核查或检查，一旦哪个月被核查或检查，这条消失）
    private String tz3 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M12;E104-1_M6;\",\"formulaBeans\":[{\"formula\":\"E204-1_M12/E104-1_M6=1.2|50\"},{\"formula\":\"E204-1_M12/E104-1_M6>1.2|((((E204-1_M12/E104-1_M6)-1.2)/0.01)*2)+50\"}]}]";
    //上一年度12个月的本月零售额(e204-1 M20)相加与年报零售额(e104-1 M11)的差异率超过20%，计50分。每超过20%一个百分点，多加2分。（一年以内这一条在4月份年报出来的时候使用，然后一直保留这家企业被核查或检查，一旦哪个月被核查或检查，这条消失）
    private String tz4 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M20;E104-1_M11;\",\"formulaBeans\":[{\"formula\":\"E204-1_M20/E104-1_M11=1.2|50\"},{\"formula\":\"E204-1_M20/E104-1_M11>1.2|((((E204-1_M20/E104-1_M11)-1.2)/0.01)*2)+50\"}]}]";
    //本月商品销售额(e204-1 M12)+上个月的1-本月商品销售额(e204-1 M13)与1-本月商品销售额(e204-1 M13)的差异率为20%，计50分。每超过20%一个百分点，多加1分。如差异率为21%，分数51；增速为25%，分数55分，增速30%，分数60分；增速11.5%，分数为51.5分。
    private String tz1 = "[{\"dimension\":\"C\",\"field\":\"E204-1_M12;E204-1_M13#m;E204-1_M13;\",\"formulaBeans\":[{\"formula\":\"(((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)=0.2|50\"},{\"formula\":\"(((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)>0.2|((((E204-1_M12+E204-1_M13#m)/E204-1_M13)-1)-0.2)/0.01*1+50\"}]}]";


    public WarnJiSuanBean warnJiSuan = null;


    //通用表
    List<Map<String, Object>> E203ls = null;
    List<Map<String, Object>> E204ls = null;

    List<Map<String, Object>> TZSum1 = null;
    List<Map<String, Object>> TZSum2 = null;
    List<Map<String, Object>> E104ls = null;
    List<Map<String, Object>> S104ls = null;
    List<Map<String, Object>> S204_1ls = null;
    List<Map<String, Object>> E204_1ls = null;
    List<Map<String, Object>> F201_1ls = null;

    List<Map<String, Object>> HJ_ls = null;
    List<Map<String, Object>> HJN_ls = null;


    @Override
    public Integer jisuan(WarnJiSuanBean warnJiSuan) {
        System.out.println("开始："+warnJiSuan.getSurveyobjectid());
        this.warnJiSuan = warnJiSuan;
        this.jdbcDao = warnJiSuan.getJdbcDao();
        GetOrganization getOrganization = new GetOrganization(jdbcDao);


        Long startTime = System.currentTimeMillis();

        /**
         * 当前所用所有list一次取出
         */
        E203ls = getOrganization.getFieldName("E203", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        E204ls = getOrganization.getFieldName("E204-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());
        E104ls = getOrganization.getFieldName("E104-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());

        HJ_ls =  getOrganization.gethj("et_8600000002021110107720376", warnJiSuan.getSurveyobjectid());
        //年报
        HJN_ls =  getOrganization.gethj("et_8600000002021110107720961", warnJiSuan.getSurveyobjectid());


        //S104ls = getOrganization.getFieldName("S104-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());

        TZSum1 = jdbcDao.loadList(String.format("SELECT SUM(t1.M12) AS M12, t1.M30, t1.time_code FROM ( SELECT M30, IFNULL(M12, 0) AS M12, M32 , time_code FROM `e204-1` WHERE M30 = '%s' ) t1 GROUP BY t1.M30, LEFT(time_code, 4)", warnJiSuan.getSurveyobjectid()));
        TZSum2 = jdbcDao.loadList(String.format("SELECT SUM(t1.M20) AS M20, t1.M30, t1.time_code FROM ( SELECT M30, IFNULL(M20, 0) AS M20, M32 , time_code FROM `e204-1` WHERE M30 = '%s' ) t1 GROUP BY t1.M30, LEFT(time_code, 4)", warnJiSuan.getSurveyobjectid()));

        Date lastTime = warnJiSuan.getLastTime();

        //S204_1ls = getOrganization.getFieldName_TJRY("S204-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid(), lastTime);
        E204_1ls = getOrganization.getFieldName_TJRY("E204-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid(), lastTime);
        F201_1ls = getOrganization.getFieldName("201-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());

        warnJiSuan.setF201_1ls(F201_1ls);

      Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - startTime);
        System.out.println("查询时间："+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));

        String year_is_month = getOrganization.gethc(warnJiSuan.getSurveyobjectid());//核查时间
        warnJiSuan.setYear_is_month(year_is_month);


        //startTime = System.currentTimeMillis();

        HjOperator hjOperator = new HjOperator(jdbcDao, warnJiSuan);


        List<OperatorVal> listzs1 = OperatorXmlUtil.readXml("maoyi_zs1");




        List<OperatorVal> list = JSONObject.parseArray(xgfd, OperatorVal.class);
        //修改一
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_WEIGHT.getCode());//风险因子类型
        hjOperator.jisuan(HJ_ls, list,"本年;本月;商品销售额;千元");
        hjOperator.jisuan(HJ_ls, list,"本年;1-本月;商品销售额;千元");
        hjOperator.jisuan(HJ_ls, list,"本年;本月;零售额;千元");
        hjOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");
        hjOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");

        warnJiSuan.setIs_hc(true);
        hjOperator.jisuan(HJN_ls, list,"本年;商品销售额;零售额;千元");
        hjOperator.jisuan(HJN_ls, list,"本年;服务营业额;千元");



        list = JSONObject.parseArray(xgcs, OperatorVal.class);
        HjNumOperator  hjNumOperator = new HjNumOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_MONTH_DATA.getCode());//风险因子类型
        warnJiSuan.setIs_hc(false);
        hjNumOperator.jisuan(HJ_ls, list,"本年;本月;商品销售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;1-本月;商品销售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;本月;零售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;1-本月;零售额;千元");

        warnJiSuan.setIs_hc(true);
        warnJiSuan.setWarnType(RuleEnum.TRADE_EDIT_FREQUENCY_FACTOR_YEAR_DATA.getCode());//风险因子类型
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;零售额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;服务营业额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;批发额;千元");
        hjNumOperator.jisuan(HJ_ls, list,"本年;商品销售额;批发额;千元");
        //










        //启动季报与月报匹配算子
        JtYOperator jtYOperator = new JtYOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(pp1, OperatorVal.class);
        //匹配一
        warnJiSuan.setIs_hc(false);

        warnJiSuan.setWarnType(RuleEnum.TRADE_MATCH_FACTOR_DATA.getCode());//风险因子类型
        jtYOperator.jisuan(E203ls, E204ls, list);


        //启动同比增长算子
        ZsOperator zsOperator = new ZsOperator(jdbcDao, warnJiSuan);
        list = OperatorXmlUtil.readXml("maoyi_zs1");
        //JSONObject.parseArray(zs1, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_GROW_FACTOR_DATA.getCode());//风险因子类型
        zsOperator.jisuan(E204ls, list);



        list = JSONObject.parseArray(zs2, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_GROW_FACTOR_DATA1.getCode());//风险因子类型
        zsOperator.jisuan(E204ls, list);

        //启动调整1算子
        Tz1Operator tz1Operator = new Tz1Operator(jdbcDao, warnJiSuan);
        //调整1
        list = JSONObject.parseArray(tz1, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_MONTH_DATA1.getCode());//风险因子类型
        tz1Operator.jisuan(E204ls, list);
        //调整2
        SyOperator syOperator = new SyOperator(jdbcDao, warnJiSuan);

        list = JSONObject.parseArray(tz2, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_MONTH_DATA2.getCode());//风险因子类型
        syOperator.jisuan(E204ls, list);

        NbOperator nbOperator = new NbOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(tz3, OperatorVal.class);
        warnJiSuan.setIs_hc(true);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_YEAR_DATA1.getCode());//风险因子类型
        nbOperator.jisuan(E104ls, TZSum1, list);

        list = JSONObject.parseArray(tz4, OperatorVal.class);
        warnJiSuan.setIs_hc(true);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ADJUSTMENT_FACTOR_YEAR_DATA2.getCode());//风险因子类型
        nbOperator.jisuan(E104ls, TZSum2, list);


        //启动统计人员变更计算
        TjryOperator tjryOperator = new TjryOperator(jdbcDao, warnJiSuan);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_STATISTICS_FACTOR_DATA.getCode());//风险因子类型
        //tjryOperator.jisuan(S204_1ls, "M40", "B");
        tjryOperator.jisuan(E204_1ls, "M5", "B");


        //小升规  贸易-批零
        XsgOperator xsgOperator = new XsgOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(pl, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA1.getCode());//风险因子类型
        zsOperator.jisuan(E104ls, list);


        endTime = System.currentTimeMillis();
         tempTime = (endTime - startTime);
        System.out.println("计算时间："+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));


        //小升规  贸易-住餐
        /*list = JSONObject.parseArray(zc, OperatorVal.class);
        warnJiSuan.setIs_hc(false);
        warnJiSuan.setWarnType(RuleEnum.TRADE_ABNORMAL_SMALL_FACTOR_DATA2.getCode());//风险因子类型
        xsgOperator.jisuanMaoyiZc(S104ls, list);*/

        //运营状态
        /*YyztOperator yyztOperator = new YyztOperator(jdbcDao, warnJiSuan);
        list = JSONObject.parseArray(yxzt, OperatorVal.class);
        yyztOperator.jisuanYyzt(F201_1ls, list);
        */


        return null;
    }
}
